package com.loveyukari.utils;

import java.io.Serializable;
import java.util.Random;

import org.apache.commons.lang3.ArrayUtils;

/**
*
*yangshaohua
*2017年8月9日上午10:58:18
**/
public class RandomCodeUtil {
	
	private static String[] numberArray = {"0","1","2","3","4","5","6","7","8","9"};
	private static String[] lowerArray = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
	private static String[] upperArray = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
	private static String[] charArray = ArrayUtils.addAll(lowerArray,upperArray);
	private static String[] nlArray = ArrayUtils.addAll(numberArray,lowerArray);
	private static String[] nuArray = ArrayUtils.addAll(numberArray,upperArray);
	private static String[] allAttay = ArrayUtils.addAll(numberArray, charArray);
	private static Random random = new Random();

	public static String randomCode(int n){
		return getCode(allAttay,n);
	}
	
	public static String randomUpperCode(int n){
		return getCode(nuArray,n);
	}
	
	public static String randomLowerCode(int n){
		return getCode(nlArray,n);
	}
	
	public static String randomNumberCode(int n){
		return getCode(numberArray,n);
	}

	private static String getCode(String[] array,int n){
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < n; i++) {
			int index = random.nextInt(array.length);
			sb.append(array[index]);
		}
		return sb.toString();
	}
	
}
